import vec2
import gfx


ANIMATION_RUN = 0, (0, 1, 2), 1
ANIMATION_WORK = 1, (0, 1, 2, 1), 1
ANIMATION_DEATH = 2, (0, 1, 2), 0
ANIMATION_HELICOPTER = 0, (0, 1, 2), 1
ANIMATION_WATER_FROM_HELICOPTER = 0, (0, 1, 2), 1

class SingleWorker(object):
    def __init__(self, spriteSheet, pos):
        self.anim = gfx.AnimatedSprite(spriteSheet)
        self.pos = vec2.Vec2(pos)
        self.target = vec2.Vec2(pos)
        self.isDead = False
        
        self.anim.set_position(self.pos, 0)
    
    def tick(self, surface):
        # determine facing:
        facing = self.pos.x < self.target.x
        
        if self.isDead:
            # worker is dead:
            self.anim.set_animation(*ANIMATION_DEATH)
            facing = None
        elif self.pos == self.target:
            # worker is on target pos:
            self.anim.set_animation(*ANIMATION_WORK)
            facing = None
        elif self.pos.distance(self.target) <= 1:
            # worker is moving to target pos (and reaching it this turn):
            self.anim.set_animation(*ANIMATION_WORK)
            self.pos = self.target
        else:
            # worker is moving to target pos (and reaching it this turn):
            self.anim.set_animation(*ANIMATION_RUN)
            self.pos += (self.target - self.pos).setlength(1)
            
        intpos = int(self.pos.x), int(self.pos.y)
        self.anim.set_position(intpos, facing)
        self.anim.draw_frame(surface)
    
    def move_to(self, target):
        self.target = vec2.Vec2(target)
    
    def die(self):
        self.isDead = True
        self.target = self.pos # the dead go nowhere anymore
        

class Helicopter(object):
    def __init__(self, spriteSheet, waterSpriteSheet, pos):
        self.spriteSheet = spriteSheet
        self.anim = gfx.AnimatedSprite(spriteSheet)
        self.anim.set_animation(*ANIMATION_HELICOPTER)
        self.anim.set_position(pos, 0)
        self.anim.frameDelay = 6
        self.pos = pos
        self.waterAnim = gfx.AnimatedSprite(waterSpriteSheet)
        self.waterAnim.set_animation(*ANIMATION_WATER_FROM_HELICOPTER)
        
        
    def tick(self, surface):
        x, y = self.pos
        self.anim.set_position((x, y), 0)
        
        
        if x > 100:
            xWater = x + 87 -223 - 5 # these offsets are based on image layouts
            yWater = y + 227 -5
            self.waterAnim.set_position((xWater, yWater), 0)
            self.waterAnim.draw_frame(surface)
        
        self.anim.draw_frame(surface)
        
        self.pos = ((x+1) % 800, y)

        
class ReactorBuilding(object):
    def __init__(self, images, pos):
        self.images = images
        self.state = 0
        self.pos = pos
        
    def set_state(self, state):
        self.state = state
        
    def draw(self, surface):
        surface.blit(self.images[self.state], self.pos)